﻿<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
<!-- saved from url=(0033)http://research.microsoft.com/pex -->
<head><link rel="StyleSheet" href="er.common.css" type="text/css" /><script src="er.common.js" type="text/javascript"> </script><link rel="SHORTCUT ICON" href="favicon.ico" /><link rel="icon" href="favicon.ico" type="image/x-icon" /><title>C:\Users\AO\Documents\Visual Studio 2010\AutomaTones3\automatones\AutomaTones\AutomaTones\View\Launchpad View\LaunchPad.cs</title></head><body><div class="banner"></div><h2 id="top">C:\Users\AO\Documents\Visual Studio 2010\AutomaTones3\automatones\AutomaTones\AutomaTones\View\Launchpad View\LaunchPad.cs</h2><div class="toggle"><p class="copyright"><a class="usercodeundertestcovered">user code under test</a>, 
  <span class="usercodeundertestnotcovered">user code under test (not covered)</span>, 
  <a class="userortestcodecovered">user code or test</a>, 
  <span class="userortestcodenotcovered">user code or test (not covered)</span>,
  <span class="tagged">tagged</span></p><table><tr><th>Methods</th></tr><tr><th /></tr></table><div class="toggle"><a class="button" href="#ID0EL">first</a><pre class="coverage">using System;
using System.Diagnostics.Contracts;
using AutomaTones.HW_Sense_Compute;
using AutomaTones.Model.Automaton_Model;
using AutomaTones.Model.MIDI;
using Midi;
using Microsoft.Pex.Framework;

namespace AutomaTones.View.Launchpad_View {
    /// &lt;summary&gt;
    /// Launchpad holds all of our functionality needed to visualise our program on the Launchpad.
    /// &lt;/summary&gt;
    /// &lt;author&gt;Anders Bech Mellson, anbh@itu.dk&lt;/author&gt;
    [PexClass]
    class LaunchPad : IHardwareView {
        <span id="ID0EL"><a class="button" href="#top">top</a>|<a class="button" href="#ID0EKHAC">next</a></span><a class="userortestcodecovered">private readonly int[,] _gridWithMidiValues = new int[8, 8];</a>
        <a class="userortestcodecovered">private Pitch _root = Pitch.CNeg1;</a>
        <a class="userortestcodecovered">private int _rapidGridSize=0;</a>

        /// &lt;summary&gt;
        /// Constructer creates a useable Launchpad.
        /// A 2 dimensional int array used to translate from x,y coordinates into Midi NoteOn messages is constructed.
        /// This is needed to be able to say ColorPad(x,y) and then the _gridWithMidiValues grid is used to convert
        /// the x &amp; y back into the Midi Values used to color the pads on the Launchpad.
        /// 
        /// The constructor also opens the Launchpads MidiOut driver.
        /// &lt;/summary&gt;
        <a class="userortestcodecovered">public LaunchPad()</a> <a class="userortestcodecovered">{</a>
            <a class="userortestcodecovered">int x = 0;</a>
            <a class="userortestcodecovered">int y = 0;</a>
            for (<a class="userortestcodecovered">int i = 1;</a> <a class="userortestcodecovered">i &lt;= 120</a>; ) <a class="userortestcodecovered">{</a>
                <a class="userortestcodecovered">if (i % 8 == 0)</a> <a class="userortestcodecovered">{</a>
                    <a class="userortestcodecovered">_gridWithMidiValues[x, y] = i - 1;</a>
                    <a class="userortestcodecovered">i += 9;</a>
                    <a class="userortestcodecovered">y++;</a>
                    <a class="userortestcodecovered">x = 0;</a>
                <a class="userortestcodecovered">}</a>
                else <a class="userortestcodecovered">{</a>
                    <a class="userortestcodecovered">_gridWithMidiValues[x, y] = i - 1;</a>
                    <a class="userortestcodecovered">x++;</a>
                    <a class="userortestcodecovered">i++;</a>
                <a class="userortestcodecovered">}</a>
            <a class="userortestcodecovered">}</a>
            <a class="tagged" title="call to uninstrumented method OutputDevice.Open()">ConnectLaunchpad.Out.Open();</a>
        <a class="userortestcodecovered">}</a>

        /// &lt;summary&gt;
        /// Change color of a pad on the Launchpad.
        /// &lt;/summary&gt;
        /// &lt;param name="x"&gt;The x coordinate on the Launchpad. Valid values are from 1 to 8.&lt;/param&gt;
        /// &lt;param name="y"&gt;The y coordinate on the Launchpad. Valid values are from 1 to 8.&lt;/param&gt;
        /// &lt;param name="color"&gt;The PadColor you would like to be lit on the Launchpad.&lt;/param&gt;
        private void ColorPad(int x, int y, PadColor color) {
            Contract.Requires(x &gt;= 0 &amp;&amp; x &lt;= 7);
            Contract.Requires(y &gt;= 0 &amp;&amp; y &lt;= 7);
            if (x &lt; 8 &amp;&amp; y &lt; 8) {
                _root = (Pitch)(_gridWithMidiValues[x, y]);
                ConnectLaunchpad.Out.SendNoteOn(Channel.Channel1, _root, (int)color);
            }
        }

        /// &lt;summary&gt;
        /// Sets the Launchpad in a default state, and colors the statechange pads.
        /// The 64 pad field is being switched off.
        /// &lt;/summary&gt;
        [PexMethod]
        public void ClearGrid() <a class="button" href="#ID0EL">prev</a>|<span id="ID0EKHAC"><a class="button" href="#top">top</a>|<a class="button" href="#ID0E4LAC">next</a></span><a class="userortestcodecovered">{</a>
            // Special Midi message that the Launchpad translates into a reset message.
            <a class="tagged" title="call to uninstrumented method OutputDevice.SendControlChange(Channel, Control, Int32)">ConnectLaunchpad.Out.SendControlChange(Channel.Channel1, Control.ModulationWheel - 1, 0);</a>
            <a class="userortestcodecovered">ColorStateButtons();</a>

            // OK Button
            <a class="userortestcodecovered">ColorControlButton(0,PadColor.FullGreen);</a>
            // Clear Button
            <a class="userortestcodecovered">ColorControlButton(7, PadColor.FullRed);</a>
        <a class="userortestcodecovered">}</a>

        /// &lt;summary&gt;
        /// Colors all the Statechange pad's on the right side of the Launchpad
        /// &lt;/summary&gt;
        [PexMethod]
        public void ColorStateButtons() <a class="button" href="#ID0EL">prev</a>|<span id="ID0E4LAC"><a class="button" href="#top">top</a>|<a class="button" href="#ID0EDDAE">next</a></span><a class="userortestcodecovered">{</a>
            <a class="tagged" title="call to uninstrumented method OutputDevice.SendNoteOn(Channel, Pitch, Int32)">ConnectLaunchpad.Out.SendNoteOn(Channel.Channel1, Pitch.CNeg1 + 24, (int)PadColor.FullRed);</a>
            <a class="userortestcodecovered">ConnectLaunchpad.Out.SendNoteOn(Channel.Channel1, Pitch.CNeg1 + 40, (int)PadColor.DimRed);</a>
            <a class="userortestcodecovered">ConnectLaunchpad.Out.SendNoteOn(Channel.Channel1, Pitch.CNeg1 + 56, (int)PadColor.FullAmber);</a>
            <a class="userortestcodecovered">ConnectLaunchpad.Out.SendNoteOn(Channel.Channel1, Pitch.CNeg1 + 72, (int)PadColor.DimAmber);</a>
            <a class="userortestcodecovered">ConnectLaunchpad.Out.SendNoteOn(Channel.Channel1, Pitch.CNeg1 + 88, (int)PadColor.FullGreen);</a>
            <a class="userortestcodecovered">ConnectLaunchpad.Out.SendNoteOn(Channel.Channel1, Pitch.CNeg1 + 104, (int)PadColor.DimGreen);</a>
        <a class="userortestcodecovered">}</a>

        /// &lt;summary&gt;
        /// Set the color of a single pad on the Launchpad.
        /// The method will automaticly convert from a cells status to a color on the Launchpad.
        /// &lt;/summary&gt;
        /// &lt;param name="x"&gt;The x coordinate.&lt;/param&gt;
        /// &lt;param name="y"&gt;The y coordinate.&lt;/param&gt;
        /// &lt;param name="state"&gt;The state of the cell.&lt;/param&gt;
        [PexMethod]
        public void SetCell(int x, int y, State state) {
            Contract.Requires(x &gt;= 0 &amp;&amp; x &lt;= 7);
            Contract.Requires(y &gt;= 0 &amp;&amp; y &lt;= 7);
            Contract.Requires(state != null);
            ColorPad(x,y,LaunchpadModel.ConvertStateToColor(state));
        }

        /// &lt;summary&gt;
        /// Updates the Launchpad faster than the SetCell method. It does so by setting two LED's at the same time instead of one.
        /// &lt;/summary&gt;
        /// &lt;param name="state1"&gt;&lt;/param&gt;
        /// &lt;param name="state2"&gt;&lt;/param&gt;
        [PexMethod]
        public void RapidUpdate(State state1, State state2) {
            Contract.Requires(state1!=null &amp;&amp; state2 != null);
            // RapidUpdate Midi message, sends two pad updates at a time. Needs a normal Midi message to return to normal functioning again - see below.
            ConnectLaunchpad.Out.SendNoteOn(Channel.Channel3, (Pitch)LaunchpadModel.ConvertStateToColor(state1), (int)LaunchpadModel.ConvertStateToColor(state2));
            
            /* Counter used to check if the entire 64 pad field has been painted.
             * If we didn't check and let the RapidUpdate go above 32, then it would start painting outside the 64 pad field.
             */
            _rapidGridSize++;
            
            // If _rapidGridSize == 32 then we know that the Launchpads 64 pads are updated (2 pads per message times 32).
            if (_rapidGridSize == 32) {
                _rapidGridSize = 0;
                /* Coloring the upperleft button on the Launchpad green.
                 * Only doing this because the Launchpad needs another midimessage than the RapidUpdate
                 * after each runthrough inorder to function normally again.
                 */
                ColorControlButton(0, PadColor.FullGreen);
            }
        }

        /// &lt;summary&gt;
        /// Updates the entire surface of the Launchpad in one go.
        /// &lt;/summary&gt;
        /// &lt;param name="stateGrid"&gt;Grid to be painted on the Launchpad.&lt;/param&gt;
        [PexMethod]
        public void SetGrid(State[,] stateGrid) {
            Contract.Requires(stateGrid!=null);
            Contract.Requires(stateGrid.GetLength(0)&lt;=8 &amp;&amp; stateGrid.GetLength(1)&lt;=8);
            for (int x = 0; x &lt; stateGrid.GetLength(0); x++)
                for (int y = 0; y &lt; stateGrid.GetLength(1); y+=2)
                    RapidUpdate(stateGrid[y, x],stateGrid[y+1,x]);
        }

        /// &lt;summary&gt;
        /// Colors the toprow buttons on the Launchpad.
        /// &lt;/summary&gt;
        /// &lt;param name="button"&gt;The button from left to right you wish to color.&lt;/param&gt;
        /// &lt;param name="color"&gt;The color you wish the button to get.&lt;/param&gt;
        [PexMethod]
        public void ColorControlButton(int button, PadColor color) <a class="button" href="#ID0EL">prev</a>|<span id="ID0EDDAE"><a class="button" href="#top">top</a></span><a class="userortestcodecovered">{</a>
            //Contract.Requires(button &gt;=1 &amp;&amp; button &lt;= 8);
            <a class="userortestcodecovered">ConnectLaunchpad.Out.SendControlChange(Channel.Channel1, Control.Pan + 94+button, (int)color);</a>
        <a class="userortestcodecovered">}</a>

        /// &lt;summary&gt;
        /// Since the Launchpad can only show rectangular grids,
        /// this method is not implemented.
        /// 
        /// It is inherited from IHardwareView made by the brilliant Anders Høst Kjærgaard.
        /// &lt;/summary&gt;
        [PexMethod]
        public void SetArbitraryGrid(State[,] stateGrid) {
            Contract.Requires(stateGrid!=null);
            Console.WriteLine("Hardware sets arbitrary grid (Or at least it want's to, but it's so sad - since it cant..).");
        }

        [ContractInvariantMethod]
        private void Invariant() {
            Contract.Invariant(_rapidGridSize &gt;= 0 &amp;&amp; _rapidGridSize &lt; 32);
        }
    }
}
</pre></div></div><hr /><table width="100%"><tr><td valign="top"><span class="copyright">Copyright (c) Microsoft Corporation. All rights reserved.</span><br /><span class="button" onclick="copySourceToClipboard()">Copy full source to clipboard</span></td></tr></table><div id="debugdiv"> </div></body></html>